<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { deleteUserByIdUsingDelete, pageUsersUsingPost } from '@/api/yonghumokuai.ts'
import { message } from 'ant-design-vue'
import UserEditPage from '@/pages/user/UserEditPage.vue'

const columns = [
  {
    title: 'ID',
    dataIndex: 'userId',
    align: 'center',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
    align: 'center',
  },
  {
    title: '昵称',
    dataIndex: 'userName',
    align: 'center',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
    align: 'center',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
    align: 'center',
  },
  {
    title: '角色',
    dataIndex: 'userRole',
    align: 'center',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    align: 'center',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    align: 'center',
  },
  {
    title: '操作',
    dataIndex: 'action',
    align: 'center',
  },
]

const total = ref<number | undefined>(0)
const data = ref<API.UserVo[] | undefined>([])
const visible = ref<boolean>(false)

// 搜索条件
const searchParams = ref<API.UserPageRequest>({
  current: 1,
  pageSize: 10,
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.value.current ?? 1,
    pageSize: searchParams.value.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

const loadTableData = async () => {
  const res = await pageUsersUsingPost({
    ...searchParams.value,
  })
  if (res.data.code === 0 && res.data.data) {
    data.value = res.data.data.records
    total.value = res.data.data.total
  } else {
    message.error(res.data.message)
  }
}

// 表格变化处理
const doTableChange = (page: any) => {
  searchParams.value.current = page.current
  searchParams.value.pageSize = page.pageSize
  loadTableData()
}

// 搜索
const search = () => {
  searchParams.value.current = 1
  loadTableData()
}

// 删除用户
const doDelete = async (userId: string) => {
  if (!userId) {
    return
  }
  const res = await deleteUserByIdUsingDelete({
    userId: userId,
  })
  if (res.data.code === 0) {
    message.success('删除成功')
    await loadTableData()
  } else {
    message.error(res.data.message)
  }
}

// 打开对话框
const openModal = () => {
  visible.value = true
}

// 关闭对话框
const closeModal = () => {
  visible.value = false
}

onMounted(() => loadTableData())
</script>

<template>
  <div id="userManagePage">
    <a-form layout="inline" :model="searchParams" style="margin-bottom: 20px" @finish="search">
      <a-form-item label="账号">
        <a-input v-model:value="searchParams.userAccount" placeholder="请输入账号" allow-clear />
      </a-form-item>
      <a-form-item label="昵称">
        <a-input v-model:value="searchParams.userName" placeholder="请输入昵称" allow-clear />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-table
      :columns="columns"
      :data-source="data"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :src="record.userAvatar" :width="100" />
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 0">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">普通用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'action'">
          <a-space>
            <a-button type="primary" size="small" ghost @click="openModal">编辑</a-button>
            <a-popconfirm
              title="确定删除这条记录吗？"
              ok-text="确定"
              cancel-text="取消"
              @confirm="doDelete(record.userId)"
            >
              <a-button danger size="small" v-if="record.userRole === 1">删除</a-button>
            </a-popconfirm>
          </a-space>
        </template>
      </template>
    </a-table>
    <user-edit-page :visible="visible" @update:visible="closeModal" />
  </div>
</template>

<style scoped></style>
